﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Blizzard.Contracts;

namespace Blizzard.Container
{
    class RelationInstance
    {
        RelationDefinition rd;
        IDataObject _obj1;
        IDataObject _obj2;

        /// <summary>
        /// Correspondency of obj1 to Rd.TypeRole1 and obj2 to Rd.TypeRole2 is garanteed.
        /// </summary>
        /// <param name="Rd"></param>
        /// <param name="obj1"></param>
        /// <param name="obj2"></param>
        public RelationInstance(RelationDefinition Rd , IDataObject obj1 , IDataObject obj2)
        {
            rd = Rd;

            Type obj1Type = obj1.GetType();
            Type obj2Type = obj2.GetType();

            if (obj1Type == rd.TypeRole1.MyType)
            {
                _obj1 = obj1;
                _obj2 = obj2;
            }
            else
            {
                _obj1 = obj2;
                _obj2 = obj1;
            }

             
             
        }
        /// <summary>
        /// if the relationInstance if of type One to many (parent-child)this method
        /// returns the child object otherwise it returns null.
        /// </summary>
        /// <returns></returns>
        public IDataObject GetChild()
        {
            if (rd.TypeRole1.MyRole == Role.Child)
            {

                if (this.Obj1.GetType() == rd.TypeRole1.MyType)
                {
                    return Obj1;
                }
                else
                {
                    return Obj2;
                }

            }
            else if (rd.TypeRole1.MyRole == Role.Parent )
            {
                if (this.Obj1.GetType() == rd.TypeRole1.MyType)
                {
                    return Obj2;
                }
                else
                {
                    return Obj1;
                }
               
            }

            return null;
        }

        public RelationDefinition Rd
        {
            get { return rd; }
        }

        public IDataObject Obj1
        {
            get { return _obj1; }
        }
        public IDataObject Obj2
        {
            get { return _obj2; }
        }

        public RelationStatus Status { get; set; }

    }
}
